import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState()],
  state: {
    token: '',
    userInfo: {}
  },
  getters: {
  },
  mutations: {
    updateState (state, payload) {
      state.token = payload
    },
    updateUserInfo (state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    // 获取用户信息
    async getUserInfo (ctx) {
      const { data: res } = await axios.get('/my/userinfo')
      // console.log(res)
      ctx.commit('updateUserInfo', res.data)
    }
  },
  modules: {
  }
})
